package com.app.web.goods.goods;

import com.app.utils.base.http.ResultUtil;
import com.app.utils.base.j2se.Common;
import com.app.utils.base.j2se.StringHandler;
import com.app.utils.spring.config.SystemError;
import com.app.web.user.login.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description
 * Created by 王伟
 * Date create on 18:48 2019/11/26.
 */
@RestController
@RequestMapping("goods/goods/")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private GoodsManager goodsManager;

    @RequestMapping("{id}")
    public String goods(@RequestParam(defaultValue = "0")int role,@PathVariable("id") int id){
        Map<String,Object> goods = goodsService.goods(id,role);
        if(Common.isEmpty(goods))return ResultUtil.error(555,"暂无数据");
        /* 商家信息 */
//        Map<String, Object> hos = new HashMap<>();
//        int merchantId = Convert.objToInt(goods.get("merchant_id"));
//        hos.put("spot", MerchantManager.getMerchantById(merchantId).get("spot"));
//        hos.put("logo", MerchantManager.getMerchantById(merchantId).get("logo"));
//        hos.put("scoreNum", MerchantManager.getMerchantById(merchantId).get("scoreNum"));
//        hos.put("address", MerchantManager.getMerchantById(merchantId).get("address"));
//        hos.put("isRecommend", MerchantManager.getMerchantById(merchantId).get("isRecommend"));
//        hos.put("type", Convert.objToInt(MerchantManager.getMerchantById(merchantId).get("type"))==1?"总店":"分店");
        /* 组装返回数据 */
        Map<String, Object> data = GoodsManager.toMap(goods);
//        data.put("merchant",hos);
        /* 推荐商品项目集ID */
//        if(Common.isNotEmpty(Config.pp.get("goods.info.recommend.goods.promote")))data.put("promoteId", Config.pp.get("goods.info.recommend.goods.promote"));
        /* 组装小程序推广链接地址（小程序码生成所需参数） */
//        data.put("qrcode",String.format(Config.pp.get("goods.info.share.qrcode.param"),Config.pp.get("goods.info.share.page"),"goodsDetail",id,appid));
        return ResultUtil.success(data);
    }

    /**
     * 项目搜索页
     * @param name  项目名称
     * @param page  分页
     * @return
     */
    @RequestMapping(value = "list",params = {"!merchantId","!categoryId","name"})
    public String goodsListSearch(@RequestParam(defaultValue = "0")int role, String name, String sort,
                                  @RequestParam(defaultValue = "1") int page){
        Map<String,Object> param = new HashMap<String,Object>(){{
            put("name",name);
            put("sort",sort);
            put("role",role);
        }};
        List<Map<String,Object>> goodsList = goodsService.goodsList(param, page);
        if(Common.isEmpty(goodsList))return ResultUtil.success(0,"暂无数据");
        return ResultUtil.success(goodsList.stream().map(GoodsManager::toMap).collect(Collectors.toList()));
    }

    /**
     * 首页精选
     * @param page  分页
     * @return
     */
    @RequestMapping(value = "list",params = "categoryId=0")
    public String goodsListCategory(@RequestParam(defaultValue = "0")int role,@RequestParam(defaultValue = "1") int page, String sort){
        return promote(role,1,page,sort);
    }

    /**
     * 项目类目页
     * @param categoryId    类目ID
     * @param page  分页
     * @return
     */
    @RequestMapping(value = "list",params = "categoryId!=0")
    public String goodsListCategory(@RequestParam(defaultValue = "0")int role,int categoryId,@RequestParam(defaultValue = "") String sort,@RequestParam(defaultValue = "0") int merchantId,int page){
        Map<String,Object> param = new HashMap<String,Object>(){{
            put("categoryId",categoryId);
            put("merchantId",merchantId);
            put("sort",sort);
            put("role",role);
        }};
        List<Map<String,Object>> goodsList = goodsService.goodsList(param, page);
        if(Common.isEmpty(goodsList))return ResultUtil.success(0,"暂无数据");
        return ResultUtil.success(goodsList.stream().map(GoodsManager::toMap).collect(Collectors.toList()));
    }

    /**
     * 项目查询
     * @param merchantId    商家ID
     * @param page  分页
     * @return
     */
    @RequestMapping(value = "list",params = {"!name","!categoryId"})
    public String goodsList(@RequestParam(defaultValue = "0")int role,@RequestParam(defaultValue = "0") int merchantId,
                            @RequestParam(defaultValue = "") String sort,
                            @RequestParam(defaultValue = "1") int page){
        Map<String,Object> param = new HashMap<String,Object>(){{
            put("merchantId",merchantId);
            put("sort",sort);
            put("role",role);
        }};
        List<Map<String,Object>> goodsList = goodsService.goodsList(param, page);
        if(Common.isEmpty(goodsList))return ResultUtil.success(0,"暂无数据");
        return ResultUtil.success(goodsList.stream().map(GoodsManager::toMap).collect(Collectors.toList()));
    }

    /**
     * 项目集商品
     * @param id
     * @param page
     * @return
     */
    @RequestMapping("promote")
    public String promote(@RequestParam(defaultValue = "0") int role,int id,int page,@RequestParam(defaultValue = "") String sort){
        String ids = GoodsManager.goodsByPromote(id);
        if(Common.isEmpty(ids))return ResultUtil.success(0,"暂无数据");
        Map<String,Object> param = new HashMap<String,Object>(){{
            put("ids",ids);
            put("sort",sort);
            put("role",role);
        }};
        List<Map<String,Object>> goodsList = goodsService.goodsList(param, page);
        if(Common.isEmpty(goodsList))return ResultUtil.success(0,"暂无数据");
        return ResultUtil.success(goodsList.stream().map(GoodsManager::toMap).collect(Collectors.toList()));
    }

    /**
     * 特惠清仓
     * @param page  分页
     * @return
     */
    @RequestMapping(value = "list",params = "type==2")
    public String goodsList(@RequestParam(defaultValue = "0")int role,int type,@RequestParam(defaultValue = "1") int page){
        Map<String,Object> param = new HashMap<String,Object>(){{
            put("role",role);
            put("type",type);
        }};
        List<Map<String,Object>> goodsList = goodsService.goodsList(param, page);
        if(Common.isEmpty(goodsList))return ResultUtil.success(0,"暂无数据");
        return ResultUtil.success(goodsList.stream().map(GoodsManager::toMap).collect(Collectors.toList()));
    }

    /**
     * 活动列表
     * @return
     */
    @RequestMapping("act")
    public String act(){
        return ResultUtil.success(GoodsManager.getGoodsActList());
    }

    /**
     * 活动商品
     * @param id
     * @param page
     * @return
     */
    @RequestMapping("act/goods")
    public String actGoods(@RequestParam(defaultValue = "0") int role,int id,int page){
        String ids = GoodsManager.goodsByAct(id);
        if(Common.isEmpty(ids))return ResultUtil.success(0,"暂无数据");
        Map<String,Object> param = new HashMap<String,Object>(){{
            put("ids",ids);
            put("role",role);
        }};
        List<Map<String,Object>> goodsList = goodsService.goodsList(param, page);
        if(Common.isEmpty(goodsList))return ResultUtil.success(0,"暂无数据");
        return ResultUtil.success(goodsList.stream().map(GoodsManager::toMap).collect(Collectors.toList()));
    }

    /**
     * @ 名称:活动详情
     * @ 创建人: zhangmijia
     * @ 创建时间: 22:26
     * @ 修改人和其它信息
     * @ 参数：uid 用户id
     * @ 参数：id 活动id
     * @ 返回值:
     */
    @RequestMapping("act/info")
    public String info(int uid,int id){
        //查询活动信息
        Map<String, Object> actInfo = goodsManager.getGoodsActInfo(id);
        //活动信息不存在抛错
        if(Common.isEmpty(actInfo)){
            SystemError.wrapBs("500","活动信息不存在");
        }
        Map<String,Object> map = new HashMap<>();
        Map<String, Object> userActSubscribe = goodsService.userActSubscribe(uid, id, -1);
        map.put("join",Common.isEmpty(userActSubscribe)?0:StringHandler.getInt(userActSubscribe.get("arrival"))==0?1:2);
        map.put("title",StringHandler.getString(actInfo.get("name"),""));
        map.put("background",StringHandler.getString(actInfo.get("background"),""));
        map.put("poster",StringHandler.getString(actInfo.get("poster"),""));
        map.put("card",StringHandler.getString(actInfo.get("pic"),""));
        map.put("rule",StringHandler.getString(actInfo.get("rule"),""));
        return ResultUtil.success(map);
    }
}
